//
//  AppDelegate.m
//  lsOA
//
//  Created by 蓝水软件 on 16/7/13.
//  Copyright © 2016年 苏州蓝水有限公司. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewAndNoReadViewController.h"
#import "RootViewController.h"
#import "DrawerViewController.h"
#import "LoginRegisterViewController.h"
#import "AYCheckManager.h"

@interface AppDelegate ()<UIAlertViewDelegate>

@end

@implementation AppDelegate

//应用程序启动后，要执行的委托调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //隐藏状态栏
//    [[UIApplication sharedApplication]setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
    [self checkVision];
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    //将window推到最上方
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *userName = [defaults objectForKey:@"UserName"];
    
    
    UIViewController *currentViewController;
    
    if (userName.length > 0) {
        
        MainViewAndNoReadViewController *mainView = [[MainViewAndNoReadViewController alloc]init];
        DrawerViewController *drawView = [DrawerViewController new];
        RootViewController *rootView = [[RootViewController alloc]initWithCenterVC:mainView leftVC:drawView];
        currentViewController = rootView;
    }else{
        LoginRegisterViewController *loginView = [LoginRegisterViewController new];
        currentViewController = loginView;
        
    }
    
    [self.window makeKeyAndVisible];
    self.window.rootViewController = currentViewController;
    
    /**
     *  实时监测手机网络状况
     */
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:{
                [XHToast showBottomWithText:@"未知网络"];
                [self sendNotifiCation:@"1"];
            }
                break;
            case AFNetworkReachabilityStatusNotReachable:{
                UIAlertView * aler = [[UIAlertView alloc]initWithTitle:@"网络不可用！请检查网络连接。" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置", nil];
                aler.delegate = self;
                [aler show];
                [self sendNotifiCation:@"-1"];
            }
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:{
                [XHToast showBottomWithText:@"移动网络"];
                [self sendNotifiCation:@"1"];
            }
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:{
                [XHToast showBottomWithText:@"WIFI环境下"];
                [self sendNotifiCation:@"1"];
            }
                break;
            default:{
                [XHToast showBottomWithText:@"模拟器"];
            }
                break;
        }
        
    }];
    [manager startMonitoring];
    return YES;
}
/**
 *  无网络提示警告框 按钮点击事件
 *
 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        NSURL * url = [NSURL URLWithString:@"prefs:root=WIFI"];
        if([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }
    }
}
/**
 *  发送通知
 */
- (void)sendNotifiCation:(NSString *)isConnection{
    
    NSNotification *noNetWorkNotifi = [NSNotification notificationWithName:@"noNetWork" object:isConnection userInfo:nil];
    [[NSNotificationCenter defaultCenter] postNotification:noNetWorkNotifi];

}

//版本检测
- (void)checkVision{
    AYCheckManager *versionManager = [AYCheckManager sharedCheckManager];
    versionManager.countryAbbreviation = @"cn";
    [versionManager checkVersion];
    
}



//应用程序将要由活动状态切换到非活动状态时执行的委托调用，如按下home 按钮，返回主屏幕，或全屏之间切换应用程序等。
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

//在应用程序已进入后台程序时，要执行的委托调用。所以要设置后台继续运行，则在这个函数里面设置即可。
- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

//在应用程序将要进入前台时(被激活)，要执行的委托调用，与applicationWillResignActive 方法相对应。

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

//在应用程序已被激活后，要执行的委托调用，刚好与  applicationDidEnterBackground 方法相对应。
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
//在应用程序要完全退出的时候，要执行的委托调用。
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
